﻿@model LbangCMS.Areas.Admin.Models.ProductModel
@{
    ViewBag.Title = "EditContent";
    Layout = "~/Areas/Admin/Views/Shared/_FormLayout.cshtml";
}

@section head{
    <style type="text/css">
        div.w150 {
            width: 150px;
        }

        div.w350 {
            width: 350px;
        }
    </style>
}
<div class="panel">
    @using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { @id = "form", @class = "form-horizontal" }))
    {
        if (Model != null)
        {
            <input type="hidden" name="ID" value="@Model.ID" />
        }
        <table class="datatable" style="width:100%">
            <tr>
                <td width="100px">分类</td>
                <td>
                    <div class="w150">
                        <select name="CategoryId">
                            @if (ViewBag.Categories != null)
                            {
                                <option value="">请选择</option>
                                foreach (LbangCMS.Areas.Admin.Models.ProductCategoryModel item in (List<LbangCMS.Areas.Admin.Models.ProductCategoryModel>)ViewBag.Categories)
                                {
                                    if (Model!=null&&Model.CategoryId == item.ID)
                                    {
                                        <option value="@item.ID" selected="selected">@item.Name</option>
                                    }
                                    else
                                    {
                                        <option value="@item.ID">@item.Name</option>
                                    }

                                }

                            }
                           
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td width="100px">名称</td>
                <td>
                    <div class="w350">
                        @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
                    </div>
                </td>
            </tr>
          
            <tr>
                <td width="100px">封面图片</td>
                <td>
                    @Html.TextBoxFor(model => model.ImageSrc, new { @class = "form-control" })
                </td>
            </tr>
            <tr>
                <td width="100px">是否推荐</td>
                <td>
                   @if (Model != null && Model.IsRecommend)
                   {
                    <input type="checkbox" name="IsRecommend" checked="checked" />
                   }
                   else
                   {
                    <input type="checkbox" name="IsRecommend" />
                   }
                </td>
            </tr>
            <tr>
                <td width="100px">是否展示</td>
                <td>
                    @if(Model!=null&&Model.IsPublish)
                    {
                        <input type="checkbox" name="IsPublish" checked="checked" />
                    }
                    else
                    {
                        <input type="checkbox" name="IsPublish" />
                    }
                </td>
            </tr>
            <tr>
                <td width="100px">详细信息</td>
                <td>
                    @Html.TextAreaFor(model => model.Description, new { @class = "form-control", @rows = "20", @width = "100%" })
                </td>
            </tr>
            
            <tr>
                <td width="100px"></td>
                <td><input type="button" id="submit" class="btn btn-primary" onclick="submitForm()" value="保存"></td>
            </tr>
        </table>
    }
</div>
@section script {
<script src="~/Areas/Admin/Content/libs/zui/lib/kindeditor/kindeditor.min.js"></script>
<script src="~/Areas/Admin/Content/libs/zui/lib/kindeditor/lang/zh_CN.js"></script>
    <script type="text/javascript">
        var editor;
        KindEditor.ready(function (K) {
            editor = K.create('textarea[name="Description"]', {
                resizeType: 1,
                allowPreviewEmoticons: false,
                allowImageUpload: false,
                width: '100%',
                items: [
                    'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                    'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist', '|', 'image','media', 'link']
            });
        });
        $().ready(function () {
            $("#form").validate({
                rules: {
                    CategoryId: { required: true },
                    Name: {
                        required: true
                    },
                    ImageSrc: {
                        required: true
                    }
                },
                messages: {
                    CategoryId:{required: "请选择分类"},
                    Name: {
                        required: "请输入标题",
                    },
                    ImageSrc: {
                        required: "请输入封面地址",
                    }
                },
                onfocusout: function(element) { $(element).valid(); },
                errorPlacement: function (error, element) {
                    if (element.is(":radio")) {
                        error.appendTo(element.parent());
                    }
                    if (element.is(":checkbox"))
                        error.appendTo(element.parent());
                    else
                        error.appendTo(element.parent());
                }

            });
        })

        function submitForm(msg) {
            editor.sync()
            com.submitForm({
                isValid: true,
                $form: $("#form"),
                success: function (result) {
                    if (result.success) {
                        layer.msg("保存成功");
                        parent.reload_data();
                        setTimeout("parent.layer.closeAll('iframe');", 500);
                    } else {
                        layer.alert(result.msg);
                    }
                }
            });
        }
    </script>
}
